package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ServiceSearchReq;
import sz.szsmk.citizencard.model.network.bean.request.ShopListReq;
import sz.szsmk.citizencard.model.network.bean.response.QuryShopRegionAppRes;
import sz.szsmk.citizencard.model.network.bean.response.QuryShopTypeAppRes;
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.mvp.contract.IBusnessContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/27 13:31
 * description:
 */
public class BusinessPresenter extends IBasePresenter
        implements IBusnessContact.IBusnessPresenter<IBusnessContact.IBusnessView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public BusinessPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getShopList(String serviceType, String areaType, String distanceType, int pager, int type) {
        ShopListReq shopListReq = new ShopListReq();
        shopListReq.setDistance_type(distanceType);
        shopListReq.setPage_num(pager + "");
        shopListReq.setPage_size("15");
        shopListReq.setShop_area(areaType);
        shopListReq.setShop_type(serviceType);
        String json = new Gson().toJson(shopListReq);
        L.e(Constant.TAG, "shopListReq------" + json);
        mNetworkDateSource.shopList(shopListReq, new SubscribeResult<ShopListRes>() {
            @Override
            protected void onOk(ShopListRes response) {
                List<ShopListRes.ShopListInfosBean> shopListInfos = response.getShopListInfos();
                if (shopListInfos != null) {
                    ((IBusnessContact.IBusnessView) mView).getShopingSucc(shopListInfos);
                }
            }

            @Override
            protected void onFailure(ShopListRes response) {
                super.onFailure(response);
                ((IBusnessContact.IBusnessView) mView).getShopingFail(response.getMessage());

            }

            @Override
            protected void onNext(ShopListRes response) {
                super.onNext(response);
                ((IBusnessContact.IBusnessView) mView).getShopingFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IBusnessContact.IBusnessView) mView).getShopingFail(throwable.getMessage());

            }
        });
    }

    @Override
    public void shopSearch(String keyword) {
        ServiceSearchReq serviceSearchReq = new ServiceSearchReq();
        serviceSearchReq.setKeyWords(keyword);
        serviceSearchReq.setPage_num("1");
        serviceSearchReq.setPage_size("15");
        mNetworkDateSource.shopSearch(serviceSearchReq, new SubscribeResult<ShopListRes>() {
            @Override
            protected void onOk(ShopListRes response) {
                List<ShopListRes.ShopListInfosBean> shopListInfos = response.getShopListInfos();
                if (shopListInfos != null) {
                    ((IBusnessContact.IBusnessView) mView).shopSearchSucc(shopListInfos);
                }
            }

            @Override
            protected void onFailure(ShopListRes response) {
                super.onFailure(response);
                ((IBusnessContact.IBusnessView) mView).shopSearchFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IBusnessContact.IBusnessView) mView).shopSearchFail(throwable.getMessage());

            }
        });
    }

    @Override
    public void quryShopRegionApp() {
        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/interface/quryShopRegionApp")
                .addParams("origin", "android")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            L.e(Constant.TAG, "ServiceTypeRes----" + response);
                            QuryShopRegionAppRes serviceTypeRes = new Gson().fromJson(response, QuryShopRegionAppRes.class);
                            ((IBusnessContact.IBusnessView) mView).quryShopRegionAppSucc(serviceTypeRes);
//                            ((ISerNetworkContact.ISerNetworkView) mView).queryServiceTypeAppSucc(serviceTypeRes);
                        }
                    }
                });
    }

    @Override
    public void quryShopTypeApp() {
        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/interface/quryShopTypeApp")
                .addParams("origin", "android")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            L.e(Constant.TAG, "ServiceTypeRes----" + response);
                            QuryShopTypeAppRes shopTypeAppRes = new Gson().fromJson(response, QuryShopTypeAppRes.class);
                            ((IBusnessContact.IBusnessView) mView).quryShopTypeAppSucc(shopTypeAppRes);
//                            ((ISerNetworkContact.ISerNetworkView) mView).queryServiceTypeAppSucc(serviceTypeRes);
                        }
                    }
                });
    }

}
